package com.lhn.netty.nio;

import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * filechannel
 *
 * @author lhn
 * @date 2025/05/26 16:22
 * @since 1.0.0
 **/
public class NIOFileChannel02 {

    public static void main(String[] args) throws Exception{
        //  创建一个输入流
        File  file = new File("D:\\test\\file01.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        // 通过 fileInputStream 获取对应的 fileChannel
        // 此 fileChannel 类型是 FileChannelImpl
        FileChannel fileChannel = fileInputStream.getChannel();
        // 创建缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
        // 将 fileChannel 中的数据读入到 byteBuffer
        fileChannel.read(byteBuffer);

        System.out.println(new String(byteBuffer.array()));
        fileInputStream.close();
    }

}